﻿using MediatR;
using T.Materials.API.Application.Command.MaterialStoreCommand;
using T.Materials.Repository.MaterialsInterface;

namespace T.Materials.API.Application.CommandHandler.MaterialStoreCommandHandler
{
    /// <summary>
    /// 批量删除物资入库信息命令处理器
    /// </summary>
    public class BatchDeleteMaterialStoreCommandHandler : IRequestHandler<BatchDeleteMaterialStoreCommand, int>
    {
        /// <summary>
        /// 物资与入库仓储接口
        /// </summary>
        protected readonly IMaterialStoreRepository materialStoreRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialStoreRepository">物资与入库仓储接口</param>
        public BatchDeleteMaterialStoreCommandHandler(IMaterialStoreRepository materialStoreRepository)
        {
            this.materialStoreRepository = materialStoreRepository;
        }

        /// <summary>
        /// 处理批量删除物资入库信息命令
        /// </summary>
        /// <param name="request">批量删除物资入库信息命令</param>
        /// <param name="cancellationToken">取消标记</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Handle(BatchDeleteMaterialStoreCommand request, CancellationToken cancellationToken)
        {
            var ids = request.MaterialStoreId;
            foreach (var item in ids)
            {
                var res = await materialStoreRepository.GetModelAsync(item);
                res.IsDeleted = true;
                res.DeletedBy = request.DeletedBy;
                res.DeletedDate = DateTime.Now;
                await materialStoreRepository.UpdateAsync(res);
            }
            return 1;
        }
    }
}
